"""
File: PyCharm
Author: Liangxu.CHEN
Data: 2021/1/6 0006 14:35
"""
import uvicorn
from functools import lru_cache
from fastapi import FastAPI, Depends
from fastapi.middleware.cors import CORSMiddleware
from . import config

app = FastAPI()

origins = [
    "http://localhost.tiangolo.com",
    "https://localhost.tiangolo.com",
    "http://localhost",
    "http://localhost:8080",
]


# 缓存获取配置
@lru_cache()
def get_settings():
    return config.Settings()


app.add_middleware(
    CORSMiddleware,
    allow_origins=origins,
    allow_credentials=True,
    allow_methods=["*"],
    allow_headers=["*"],
)


@app.get("/")
async def main():
    a = "a"
    b = "b" + a
    return {"message": "Hello World " + b}


@app.get("/info")
async def info(settings: config.Settings = Depends(get_settings)):
    # 获取环境配置
    return {
        "app_name": settings.app_name,
        "admin_email": settings.admin_email,
        "items_per_user": settings.items_per_user,
    }

if __name__ == '__main__':
    uvicorn.run(app, port=8011)